package cengxubianli

import (
	"hufeng/ercahshu"
	"math"
)

func largestValues(root *ercahshu.TreeNode) []int {
	queue := make([]*ercahshu.TreeNode, 0)
	queue = append(queue, root)
	ans := make([]int, 0)
	if root == nil {
		return ans
	}
	for len(queue) > 0 {
		cur := math.MinInt32
		n := len(queue)
		for i := 0; i < n; i++ {
			if queue[i].Left != nil {
				queue = append(queue, queue[i].Left)
			}
			if queue[i].Right != nil {
				queue = append(queue, queue[i].Right)
			}
			cur = max(cur, queue[i].Val)
		}
		ans = append(ans, cur)
		queue = queue[n:]
	}
	return ans
}
